<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="inet.db.tennis.TennisCategory"%>
<%@page import="inet.db.tennis.TennisCountry"%>
<%@page import="java.math.BigDecimal"%>
<%@page import="inet.db.tennis.buffer.TennisBuffer"%>
<jsp:useBean id="tennisCategoriesDAO" class="inet.db.tennis.TennisCategoryDAO" scope="session"/>
<jsp:useBean id="tennisCountryDAO" class="inet.db.tennis.TennisCountryDAO" scope="session"/>
<jsp:useBean id="tennisChampionDAO" class="inet.db.tennis.TennisChampionDAO" scope="session"/>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>Thêm giải đấu</title>
    <link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/admin/tennis/css/reset.css" media="screen" />
    <link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/admin/tennis/css/text.css" media="screen" />
    <link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/admin/tennis/css/grid.css" media="screen" />
    <link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/admin/tennis/css/layout.css" media="screen" />
    <link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/admin/tennis/css/nav.css" media="screen" />
    <!--[if IE 6]><link rel="stylesheet" type="text/css" href="css/ie6.css" media="screen" /><![endif]-->
    <!--[if IE 7]><link rel="stylesheet" type="text/css" href="css/ie.css" media="screen" /><![endif]-->
    <link href="<%= request.getContextPath() %>/admin/tennis/css/fancy-button/fancy-button.css" rel="stylesheet" type="text/css" />
    <!--Jquery UI CSS-->
    <link href="<%= request.getContextPath() %>/admin/tennis/css/themes/base/jquery.ui.all.css" rel="stylesheet" type="text/css" />
    <!-- BEGIN: load jquery -->
    <script src="<%= request.getContextPath() %>/admin/tennis/js/jquery-1.6.4.min.js" type="text/javascript"></script>
    <script type="text/javascript" src="<%= request.getContextPath() %>/admin/tennis/js/jquery-ui/jquery.ui.core.min.js"></script>
    <script src="<%= request.getContextPath() %>/admin/tennis/js/jquery-ui/jquery.ui.widget.min.js" type="text/javascript"></script>
    <script src="<%= request.getContextPath() %>/admin/tennis/js/jquery-ui/jquery.ui.accordion.min.js" type="text/javascript"></script>
    <script src="<%= request.getContextPath() %>/admin/tennis/js/jquery-ui/jquery.effects.core.min.js" type="text/javascript"></script>
    <script src="<%= request.getContextPath() %>/admin/tennis/js/jquery-ui/jquery.effects.slide.min.js" type="text/javascript"></script>
    <!-- END: load jquery -->
    
    <!--Fancy Button-->
    <script src="<%= request.getContextPath() %>/admin/tennis/js/fancy-button/fancy-button.js" type="text/javascript"></script>
    <script src="<%= request.getContextPath() %>/admin/tennis/js/setup.js" type="text/javascript"></script>
    <script type="text/javascript">
    	function f_add_champion() {
    		if(document.formAddChampion.txtName.value==''){
				document.getElementById("lblName").innerHTML = 'Nhập tên giải đấu';
				document.formAddChampion.txtName.focus();
				return false;
			}else{
				document.getElementById("lblName").innerHTML = '';
			}
			
    		if(document.formAddChampion.txtCode.value==''){
				document.getElementById("lblCode").innerHTML = 'Nhập mã giải đấu';
				document.formAddChampion.txtCode.focus();
				return false;
			}else{
				document.getElementById("lblCode").innerHTML = '';
			}
			
    		document.formAddChampion.action.value = "CREATE";
    		document.formAddChampion.submit();
    	}
    	function disableEnterKey(e)
		{
		     var key;
		     if(window.event)
		          key = window.event.keyCode;//IE
		     else
		          key = e.which;//firefox
		     if(key == 13)
		        f_add_category();
		}
    </script>
</head>
<body>
    <div class="container_12">
        <%@ include file="../include/header.jsp" %>
        <% 
        	// set Character Encoding before any getParameter() methods.
		    request.setCharacterEncoding("UTF-8");
		    response.setCharacterEncoding("UTF-8");
		    String sAction = request.getParameter("action");
		    String sName = request.getParameter("txtName");
		    String sCode = request.getParameter("txtCode");
		    String sCateId = request.getParameter("slbCateId");
		    String sCourtType = request.getParameter("slbCourtType");
		    String sCountryId = request.getParameter("slbCountryId");
		    String sTotalSet = request.getParameter("slbTotalSet");
		    String sBracket = request.getParameter("rdoBracket");
		    
		    String sMessageErr = "";
		    String sMessageOk = "";
		    String sMessageWarning = "";
		    if(sAction != null && "CREATE".equals(sAction)) {
		    	if(sName == null || "".equals(sName) ) {
		    		sMessageWarning = "Bạn phải nhập tên giải đấu";
		    	} else if(sCode == null || "".equals(sCode) ) {
		    		sMessageWarning = "Bạn phải nhập mã giải đấu";
		    	} else {
		    		boolean isExistedChampionCode = tennisChampionDAO.isExistedChampionCode(sCode);
		    		if(!isExistedChampionCode) {
			    		boolean isOk = tennisChampionDAO.insertRow(sCode, sName, new BigDecimal(sCateId), Integer.parseInt(sCourtType), new BigDecimal(sCountryId), Integer.parseInt(sTotalSet), 0, 2, Integer.parseInt(sBracket));
			    		if(isOk) {
			    			TennisBuffer.load();
			    			sMessageOk = "Thêm giải đấu thành công";
			    		} else {
			    			sMessageErr = "Xảy ra lỗi khi thêm giải đấu";
			    		}
		    		} else {
		    			sMessageWarning = "Mã giải đấu đã tồn tại";
		    		}
		    	}
		    }
        %>
        <div class="grid_5">
            <div class="box round first fullpage">
                <h2>Thêm giải đấu</h2>
                <div class="block ">
                    <form name="formAddChampion" onKeyPress="return disableEnterKey(event)" method="post">
                    	<input type="hidden" name="action"/>
                    	<% if( !"".equals(sMessageWarning) ) { %>
                    	<div class="message warning">
                            <h5>Cảnh báo !</h5>
                            <p>
                                <%= sMessageErr %>
                            </p>
                        </div>
                        <% } %>
                    	<% if( !"".equals(sMessageOk) ) { %>
                    	<div class="message success">
                            <h5>Success!</h5>
                            <p>
                                <%= sMessageOk %>
                            </p>
                        </div>
                        <% } %>
                    	<% if( !"".equals(sMessageErr) ) { %>
                    	<div class="message error">
                            <h5>Error!</h5>
                            <p>
                                <%= sMessageErr %>
                            </p>
                        </div>
                        <% } %>
	                    <table class="form">
	                        <tr>
	                            <td class="col1">
	                                <label>Tên giải đấu</label>
	                            </td>
	                            <td class="col2">
	                                <input type="text" id="grumble" name="txtName"/>
	                                <span id="lblName" class="error"></span>
	                            </td>
	                        </tr>
	                        <tr>
	                            <td>
	                                <label>Mã giải đấu</label>
	                            </td>
	                            <td>
	                                <input type="text" id="grumble" name="txtCode"/>
	                                <span id="lblCode" class="error"></span>
	                            </td>
	                        </tr>
	                        <tr>
	                            <td>
	                                <label>Thể loại</label>
	                            </td>
	                            <td>
	                            	<% 
	                            		Vector<TennisCategory> vCategories = tennisCategoriesDAO.findAll();
	                            	%>
	                                <select id="select" name="slbCateId">
	                                	<% 
	                                		if(vCategories != null && !vCategories.isEmpty()) {
	                                			TennisCategory category = null;
	                                			for (Iterator it = vCategories.iterator(); it.hasNext(); ) {
	                                				category = (TennisCategory) it.next();
	                                				if(category == null) continue;
	                                	%>
	                                    <option value="<%= category.getId() %>"><%= category.getName() %></option>
	                                    <% 
	                                    		}
	                                    	} 
	                                    %>
	                                </select>
	                            </td>
	                        </tr>
	                        <tr>
	                            <td>
	                                <label>Loại sân</label>
	                            </td>
	                            <td>
	                                <select id="select" name="slbCourtType">
	                                    <option value="0">Hand</option>
	                                    <option value="1">Clay</option>
	                                    <option value="2">Grass</option>
	                                    <option value="3">Omni</option>
	                                    <option value="4">Carpet</option>
	                                </select>
	                            </td>
	                        </tr>
	                        <tr>
	                            <td>
	                                <label>Quốc gia</label>
	                            </td>
	                            <td>
	                                <select id="select" name="slbCountryId">
	                                	<%
	                                		Vector<TennisCountry> vCountry = tennisCountryDAO.findAll();
	                                		if(vCountry != null && !vCountry.isEmpty()) {
	                                			TennisCountry country = null;
	                                			for (Iterator it = vCountry.iterator(); it.hasNext(); ) {
	                                				country = (TennisCountry) it.next();
	                                				if(country == null) continue;
	                                	%>
	                                    <option value="<%= country.getId() %>"><%= country.getCountryName() %></option>
	                                    <% 
	                                    		}
	                                    	} 
	                                    %>
	                                </select>
	                            </td>
	                        </tr>
	                        <tr>
	                            <td>
	                                <label>Tổng số SET</label>
	                            </td>
	                            <td>
	                                <select id="select" name="slbTotalSet">
	                                    <option value="3">3 SET</option>
	                                    <option value="5">5 SET</option>
	                                </select>
	                            </td>
	                        </tr>
	                        <tr>
	                            <td>
	                                <label>Có lưu đồ</label>
	                            </td>
	                            <td>
	                                <input type="radio" name="rdoBracket" checked="checked" value="1" />
	                                Có
	                                <input type="radio" name="rdoBracket" value="0" />
	                                Không
	                            </td>
	                        </tr>
	                        <tr>
	                        	<td>
	                                <label>&nbsp;</label>
	                            </td>
	                            <td>
	                            	<button type="button" onclick="f_add_champion();" class="btn btn-grey">Thêm mới</button>
	                            	<button type="button" onclick="window.location.href='index.jsp';" class="btn btn-grey">Quay lại</button>
	                            </td>
	                        </tr>
	                    </table>
                    </form>
                </div>
            </div>
        </div>
        <div class="clear">
        </div>
    </div>
    <%@ include file="../include/footer.jsp" %>
</body>
</html>